package cn.huaqingcheng.tianshu.infrastructure.start.repository;

import cn.huaqingcheng.tianshu.core.start.model.Site;
import cn.huaqingcheng.tianshu.core.start.param.SiteParam;
import cn.huaqingcheng.tianshu.core.start.repository.SiteRepository;
import cn.huaqingcheng.tianshu.infrastructure.start.StartConvert;
import cn.huaqingcheng.tianshu.infrastructure.start.entity.SiteEntity;
import cn.huaqingcheng.tianshu.infrastructure.start.entity.SiteGroupSiteEntity;
import cn.huaqingcheng.tianshu.infrastructure.start.mapper.SiteGroupSiteMapper;
import cn.huaqingcheng.tianshu.infrastructure.start.mapper.SiteMapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * SiteRepositoryImpl
 */
@Repository
@RequiredArgsConstructor
public class SiteRepositoryImpl implements SiteRepository {

    private final SiteMapper siteMapper;

    private final SiteGroupSiteMapper groupSiteMapper;


    @Override
    public List<Site> getList(Long accountId, Long groupId) {
        List<SiteEntity> list = siteMapper.selectListByUidAndGroupId(accountId, groupId);

        return list.stream()
                .map(StartConvert.INSTANCE::toModel)
                .toList();
    }

    @Override
    public List<Site> selectListByOldIcons() {
        return siteMapper.selectListByOldIcons()
                .stream()
                .map(StartConvert.INSTANCE::toModel)
                .toList();
    }

    @Override
    public Optional<Site> selectById(Long id) {
        return Optional.ofNullable(siteMapper.selectById(id))
                .map(StartConvert.INSTANCE::toModel);
    }

    @Override
    public Map<Long, Site> selectMapByIds(List<Long> ids) {
        if (ids.isEmpty()) {
            return Map.of();
        }
        List<SiteEntity> list = siteMapper.selectByIds(ids);

        return list.stream()
                .collect(Collectors.toMap(
                        SiteEntity::getId,
                        StartConvert.INSTANCE::toModel
                ));
    }

    @Override
    public Site save(SiteParam site) {
        SiteEntity entity = StartConvert.INSTANCE.toEntity(site);
        siteMapper.insert(entity);
        saveRelationship(site, entity);

        return StartConvert.INSTANCE.toModel(entity);
    }

    private void saveRelationship(SiteParam site, SiteEntity entity) {
        groupSiteMapper.insert(new SiteGroupSiteEntity(site.getGroupId(), entity.getId()));
    }

    @Override
    public Site update(SiteParam site) {
        SiteEntity entity = StartConvert.INSTANCE.toEntity(site);
        int i = siteMapper.updateById(entity);
        if (i <= 0) {
            throw new RuntimeException("更新失败");
        }

        new LambdaUpdateChainWrapper<>(groupSiteMapper)
                .eq(SiteGroupSiteEntity::getSiteId, site.getId())
                .remove();
        saveRelationship(site, entity);

        return StartConvert.INSTANCE.toModel(entity);
    }

    @Override
    public Site update(Site site) {
        SiteEntity entity = StartConvert.INSTANCE.toEntity(site);
        int i = siteMapper.updateById(entity);
        if (i <= 0) {
            throw new RuntimeException("更新失败");
        }

        return StartConvert.INSTANCE.toModel(entity);
    }

    @Override
    public void update(List<Site> list) {
        if (list.isEmpty()) {
            return;
        }

        List<SiteEntity> entities = list.stream()
                .map(StartConvert.INSTANCE::toEntity)
                .toList();
        siteMapper.updateById(entities);
    }

    @Override
    public void remove(List<Long> id, Long accountId) {
        boolean remove = new LambdaUpdateChainWrapper<>(siteMapper)
                .eq(SiteEntity::getAccountId, accountId)
                .in(SiteEntity::getId, id)
                .remove();
        if (!remove) {
            throw new RuntimeException("删除失败");
        }
    }

}
